package com.exploreca.tourfinder.db;

import java.util.ArrayList;
import java.util.List;

import com.exploreca.tourfinder.Contact;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class FriendsDataSource {
	
	private static final String LOGCAT = "exploreca";
	
	private FriendsDBHelper dbHelper;
	private SQLiteDatabase db;
	
	public FriendsDataSource(Context context) {
		dbHelper = new FriendsDBHelper(context);
		Log.i(LOGCAT, "Database created!!!");
	}
	
	public void openDB() {
		if (db == null || !db.isOpen()) 
			db = dbHelper.getWritableDatabase();		
	}
	
	public void closeDB() {
		if (dbHelper != null)
			dbHelper.close();
	}
		
	@SuppressLint("DefaultLocale")
	public boolean check(String username) {
		openDB();
		boolean res = false;
		Cursor cursor = db.query( 
				FriendsDBHelper.TABLE_NAME, 
				new String[] { FriendsDBHelper.COLUMN_ID, 
						FriendsDBHelper.COLUMN_USERNAME, 
						FriendsDBHelper.COLUMN_PHONE }, // Which columns to return (null = all)
				null, // The columns for the WHERE clause
				null, // The values for the WHERE clause
				null, // don't group the rows
				null, // don't filter by row groups
				null // The sort order
				);
		while (cursor.moveToNext()) {
			// GET COLUMN INDICES + VALUES OF THOSE COLUMNS
			int id = cursor.getInt(cursor.getColumnIndex(FriendsDBHelper.COLUMN_ID));
			String name = cursor.getString(cursor.getColumnIndex(FriendsDBHelper.COLUMN_USERNAME));
			String phone = cursor.getString(cursor.getColumnIndex(FriendsDBHelper.COLUMN_PHONE));
			Log.i(LOGCAT, "ROW " + id + " HAS NAME " + name 
					+ " HAS PHONE " + phone
					);
			if (name.equals(username)) 
				res = true;
		}
		cursor.close();
		
		closeDB();
		
		return res;
	}
	
	public boolean insert(String username, String phone) {		
		if (username.trim().equals("") || phone.trim().equals("")) return false;
		
		if (check(username)) return false;

		openDB();

		ContentValues cv = new ContentValues();
		cv.put(FriendsDBHelper.COLUMN_USERNAME, username);
		cv.put(FriendsDBHelper.COLUMN_PHONE, phone);
		
		db.insert(FriendsDBHelper.TABLE_NAME, null, cv);
		
		closeDB();
		
		return true;
	}

	public boolean delete(String username) {
		if (username.trim().equals("")) return false;
		if (!check(username)) return false;
		
		openDB();

		try {
			db.delete(FriendsDBHelper.TABLE_NAME, FriendsDBHelper.COLUMN_USERNAME + "='" + username.toLowerCase() + "'", null);
		} catch (Exception e) {
			Log.i(LOGCAT, "Delete failed!!!");
			Log.i(LOGCAT, e.toString());
		}
		
		closeDB();
		
		return true;
	}
	
	
	public List<Contact> getAllContacts() {
		List<Contact> list = new ArrayList<Contact>();
		
		openDB();
		
		Cursor cursor = db.query( 
				FriendsDBHelper.TABLE_NAME, 
				new String[] { FriendsDBHelper.COLUMN_ID, 
						FriendsDBHelper.COLUMN_USERNAME, 
						FriendsDBHelper.COLUMN_PHONE }, // Which columns to return (null = all)
				null, // The columns for the WHERE clause
				null, // The values for the WHERE clause
				null, // don't group the rows
				null, // don't filter by row groups
				null // The sort order
				);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex(FriendsDBHelper.COLUMN_ID));
			String name = cursor.getString(cursor.getColumnIndex(FriendsDBHelper.COLUMN_USERNAME));
			String phone = cursor.getString(cursor.getColumnIndex(FriendsDBHelper.COLUMN_PHONE));
			list.add(new Contact(name, phone));
			
			Log.i(LOGCAT, id + " " + name + " " + phone);
		}
		
		cursor.close();
		
		closeDB();
		
		return list;
	}

}
